package com.bing.controller.admin;

import com.bing.bean.sys.SysUser;
import com.bing.bean.weixin.*;
import com.bing.services.AccountService;
import com.bing.services.MessageService;
import com.bing.services.NewService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Created by kaibb on 2014/11/23.
 */
@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private NewService newService;

    private Logger logger = LoggerFactory.getLogger(AdminController.class);

    @RequiresPermissions(value = "")
    public String acb() {
        return "abc";
    }

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login(@ModelAttribute("user") SysUser sysUser) {
        return "admin/login";
    }

    @RequestMapping(value = "addNews", method = RequestMethod.GET)
    public String addNews(@ModelAttribute("new") Article article) {
        return "admin/addNews";
    }

    @RequestMapping(value = "addNews", method = RequestMethod.POST)
    public String addNews(@ModelAttribute("new") Article article, BindingResult result) {
        logger.info("创建News:{}", article);
        article.setcTime(new Date());
        newService.createNew(article, null);
        return "admin/addNews";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(@Validated @ModelAttribute("user") SysUser sysUser, BindingResult result) {
        logger.info("用户{}请求登陆", sysUser.getUsername());
        if (result.hasErrors()) {
            return login(sysUser);
        }
        UsernamePasswordToken token = new UsernamePasswordToken(sysUser.getUsername(), sysUser.getPassword());
        token.setRememberMe(true);
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.login(token);

        return login(sysUser);
    }


    @RequestMapping(value = "perm/reqAuth")
    @RequiresAuthentication
    @ResponseBody
    public String reqAuth() {
        return "看到这个返回,则认证测试成功";
    }

    @RequestMapping(value = "perm/reqPerm")
    @RequiresPermissions("account:create")
    @ResponseBody
    public String reqPerm() {
        return "看到这个返回,则授权测试成功";
    }

    @RequestMapping(value = "perm/reqPerm2")
    @RequiresPermissions("vip:create")
    @ResponseBody
    public String reqPerm2() {
        return "看到这个返回,则授权测试2成功";
    }

    @RequestMapping(value = "perm/req")
    @RequiresGuest()
    @ResponseBody
    public String reqGuest() {
        return "看到这个返回,则request Guest2成功";
    }


    @RequestMapping(value = "createMenu/{url}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResultMsg createMenu(@RequestBody Menu menu, @PathVariable String url) {
        logger.info("创建菜单:{},menu:{}", url, menu);
        ResultMsg result = new ResultMsg();
        Account account = accountService.findByUrl(url);
        if (account == null || account.getAccountStatus() != AccountStatus.active) {
            result.setErrcode(401);
            result.setErrmsg("此账号未注册:" + url);
            return result;
        }
        result = messageService.createMenu(account, menu);
        return result;
    }

    @RequestMapping(value = "getMenu/{url}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public GetMenu getMenu(@PathVariable String url) {
        logger.info("获取菜单:{}", url);
        GetMenu menu = null;
        Account account = accountService.findByUrl(url);
        if (account == null || account.getAccountStatus() != AccountStatus.active) {
            return menu;
        }
        menu = messageService.getMenu(account);
        logger.info("返回菜单:{}", menu);
        return menu;
    }

    @RequestMapping(value = "deleteMenu/{url}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResultMsg deleteMenu(@PathVariable String url) {
        logger.info("删除菜单:{}", url);
        ResultMsg result = null;
        Account account = accountService.findByUrl(url);
        if (account == null || account.getAccountStatus() != AccountStatus.active) {
            result.setErrcode(401);
            result.setErrmsg("此账号未注册:" + url);
            return result;
        }
        return messageService.deleteMenu(account);
    }

}
